package com.running.base.run.config;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;


import com.running.base.run.interceptor.AuthenticationInterceptor;
import com.running.base.run.util.Constants;
import com.running.base.run.util.SessionManager;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }
    
    @Bean
    public SessionManager sessionManager() {
        return new SessionManager();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/doLogin", "/api/monitor/alive", "/api/task/audit", "/api/task/db",
                        "/api/file/download", "/api/task/work", "/api/task/month", 
                        "/login.html", "/register.html", "/api/auth/**", "/css/**", "/js/**", "/images/**");
        super.addInterceptors(registry);
    }
	
	
	// 字符串过滤
    @Bean
    FilterRegistrationBean configCharsetFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new CharacterEncodingFilter());
        registrationBean.setName("encoding-filter");
        Map<String,String> filterParams = new HashMap<>();
        filterParams.put("forceEncoding","true");
        filterParams.put("encoding","UTF-8");
        registrationBean.setInitParameters(filterParams);
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }
    
    // 消息转换器
    @Bean
    StringHttpMessageConverter stringHttpMessageConverter(){
       return new StringHttpMessageConverter(Charset.forName(Constants.CHAR_SET));
    }

    // 消息转换器
    @Bean
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        return new MappingJackson2HttpMessageConverter();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/templates/", "classpath:/static/");
        registry.addResourceHandler("/static/**").
                addResourceLocations("classpath:/static/", "file:static/");

    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  设置单个文件大小
        factory.setMaxFileSize("10240KB");//KB 或者 MB 都可以 1MB=1024KB。1KB=1024B(字节)
        /// 设置总上传文件大小
        factory.setMaxRequestSize("10240KB");//KB 或者 MB 都可以 1MB=1024KB。1KB=1024B(字节)
        return factory.createMultipartConfig();
    }

}